package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    @PostMapping("/user/save")
    public Long save(@RequestBody User user){
        return userManager.save(user);
    }

    @GetMapping("/user/findByMobile")
    public User findByMobile(String mobile){
        return userManager.findByMobile(mobile);
    }

    //发送手机验证码
    @PostMapping("/user/login")
    public void sendCode(@RequestBody Map<String,String> map){
        //获取手机号,调用manager完成发送验证码操作
        String phone = map.get("phone");
        userManager.sendCode(phone);
    }

    //注册登陆
    @PostMapping("/user/loginVerification")
    public ResponseEntity regAndLogin(@RequestBody Map<String,String> map){
        return userManager.regAndLogin(map);
    }

    //完善用户基本资料
    @PostMapping("/user/loginReginfo")
    public ResponseEntity saveUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
        return userManager.saveUserInfo(userInfo,token);
    }

    //完善用户头像
    @PostMapping({"/user/loginReginfo/head","/users/header"})
    public ResponseEntity saveUserInfoHead(MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {
        return userManager.saveUserInfoHead(headPhoto,token);
    }

    //获取用户基本信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoById(@RequestParam(required = false) Long userID,@RequestHeader("Authorization") String token){
        //1.判断userID有无值
        //2.若有值,调用manager查询数据
        if (userID != null) {
           return userManager.findUserInfoById(userID);
        }else{
            //3.若无值,从token中获取用户信息,调用manager查询数据
            User user = userManager.getUserFromToken(token);
            if (user == null) {
                return ResponseEntity.status(401).body(null);
            }
            return userManager.findUserInfoById(user.getId());
        }
    }

    //更新用户信息
    @PutMapping("/users")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        return userManager.updateUserInfo(userInfo,token);
    }

    //校验验证码
    @PostMapping("/users/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> map) {
        return userManager.checkVerificationCode(map);
    }

    //更换手机号
    @PostMapping("/users/phone")
    public ResponseEntity updatePhone(@RequestBody Map<String, String> map) {
        return userManager.updatePhone(map);
    }

    //发送验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendCode() {
        //1.获取手机号
        User user = UserHolder.get();
        String phone = user.getMobile();
        //2.调用userManger中的方法实现操作
        userManager.sendCode(phone);
    }
    //查看喜欢,互相喜欢,粉丝
    @GetMapping("/users/counts")
    public ResponseEntity counts(){
        return userManager.counts();
    }
    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    @GetMapping("/users/friends/{type}")
    public ResponseEntity counts(
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @PathVariable String type){


        return userManager.findUserInfoByPage(nickname,pageNum,pageSize,type);
    }
    //取消喜欢
    @PostMapping("/users/fans/{uid}")
    public ResponseEntity Like(@PathVariable Long uid){
        return userManager.Like(uid);
    }
    //取消喜欢
    @DeleteMapping("/users/like/{uid}")
    public ResponseEntity removeLike(@PathVariable Long uid){
        return userManager.removeLike(uid);
    }
}
